package com.yanqu.road.dao.impl.season;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.enums.eSeasonRewardType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.season.SeasonRewardData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SeasonRewardDataDaoImpl extends TempDataObjectDao<SeasonRewardData> {

    public static String getSeasonRewardDataSql = "select * from `t_u_season_reward` where `userId`=? and `systemId`=? and `seasonId`=?";

    public Map<Integer, Map<Integer, SeasonRewardData>> getSeasonRewardDataMap(long userId, int systemId, int seasonId) {
        Map<Integer, Map<Integer, SeasonRewardData>> mapMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSeasonRewardDataSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, systemId);
            call.setInt(i++, seasonId);
            rs = call.executeQuery();
            while (rs.next()) {
                SeasonRewardData data = parse(rs);

                mapMap.putIfAbsent(data.getType(), new ConcurrentHashMap<>());
                mapMap.get(data.getType()).put(data.getRewardId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return mapMap;
    }

    public static String updateSeasonRewardDataSql = "update `t_u_season_reward` set value=?,getReward=?,time=?,param=? where userId=? and systemId=? and seasonId=? and type=? and rewardId=?";

    @Override
    protected boolean updateData(SeasonRewardData seasonRewardData) {
        seasonRewardData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, seasonRewardData.getValue()));
        para.put(i++, new DbParameter(Types.BOOLEAN, seasonRewardData.isGetReward()));
        para.put(i++, new DbParameter(Types.BIGINT, seasonRewardData.getTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, seasonRewardData.getParam()));
        para.put(i++, new DbParameter(Types.BIGINT, seasonRewardData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, seasonRewardData.getSystemId()));
        para.put(i++, new DbParameter(Types.INTEGER, seasonRewardData.getSeasonId()));
        para.put(i++, new DbParameter(Types.INTEGER, seasonRewardData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, seasonRewardData.getRewardId()));
        if (getDb().executeNoneQuery(updateSeasonRewardDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSeasonRewardDataSql = "insert into `t_u_season_reward`(userId,systemId,seasonId,type,rewardId,value,getReward,time,param) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(SeasonRewardData seasonRewardData) {
        seasonRewardData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, seasonRewardData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, seasonRewardData.getSystemId()));
        para.put(i++, new DbParameter(Types.INTEGER, seasonRewardData.getSeasonId()));
        para.put(i++, new DbParameter(Types.INTEGER, seasonRewardData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, seasonRewardData.getRewardId()));
        para.put(i++, new DbParameter(Types.BIGINT, seasonRewardData.getValue()));
        para.put(i++, new DbParameter(Types.BOOLEAN, seasonRewardData.isGetReward()));
        para.put(i++, new DbParameter(Types.BIGINT, seasonRewardData.getTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, seasonRewardData.getParam()));
        if (getDb().executeNoneQuery(insertSeasonRewardDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected SeasonRewardData parseData(ResultSet rs) throws SQLException {
        SeasonRewardData seasonRewardData = new SeasonRewardData();
        seasonRewardData.setUserId(rs.getLong("userId"));
        seasonRewardData.setSystemId(rs.getInt("systemId"));
        seasonRewardData.setSeasonId(rs.getInt("seasonId"));
        seasonRewardData.setType(rs.getInt("type"));
        seasonRewardData.setRewardId(rs.getInt("rewardId"));
        seasonRewardData.setValue(rs.getLong("value"));
        seasonRewardData.setGetReward(rs.getBoolean("getReward"));
        seasonRewardData.setTime(rs.getLong("time"));
        seasonRewardData.setParam(rs.getString("param"));
        return seasonRewardData;
    }

    // ----------------------------------------------------------------

    public Map<Integer, SeasonRewardData> getWasheNoGetRewardZhanLingMap(long userId, int seasonId) {
        Map<Integer, SeasonRewardData> rewardDataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from `t_u_season_reward` where `userId`=? and `systemId`=? and `seasonId`=? and `type`=? and `getReward`=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, eSystemId.WasheActivity.getValue());
            call.setInt(i++, seasonId);
            call.setInt(i++, eSeasonRewardType.WasheGetScoreZhanLing.getIntValue());
            call.setInt(i++, 0);
            rs = call.executeQuery();
            while (rs.next()) {
                SeasonRewardData data = parse(rs);
                rewardDataMap.put(data.getRewardId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return rewardDataMap;
    }

}
